package webservices;

import com.sun.java_cup.internal.runtime.Scanner;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 *
 * @author strokyl
 */
@WebService()
public class ecrituredb {
    private final String USER_FILE_URL = "/tmp/user_ebay_des_cochons.txt";
    private final String ENCHERE_FILE_URL = "/tmp/enchere_ebay_des_cochons.txt";

    /**
     * Web service operation
     * Return true if operation successful and a matching was found
     * if no matching user nothing is changed
     */
    @WebMethod(operationName = "modifierUser")
    public boolean modifierUser(@WebParam(name = "nom")
    final String nom, @WebParam(name = "password")
    final String password) {
        FileInputStream fis;
        FileOutputStream fos;
        OutputStreamWriter out;
        StringBuilder sb = new StringBuilder();
        DataInputStream in;
        BufferedReader br;
        String line;
        String name;
        boolean result = false;
        try {
            fis = new FileInputStream(USER_FILE_URL);
            in = new DataInputStream(fis);
            br = new BufferedReader(new InputStreamReader(in));

            while((line = br.readLine()) != null) {
                name = line.split(";")[0];
                if(name.equals(nom)) {
                    sb.append(String.format("%s;%s\n", nom, password));
                    result = true;
                }
                else {
                    sb.append(line);
                    sb.append("\n");
                }
            }

            fos = new FileOutputStream(USER_FILE_URL, false);
            out = new OutputStreamWriter(fos, "UTF-8");
            out.write(sb.toString());
            out.close();
            fos.close();
        } catch (FileNotFoundException ex) {
            result = false;
        } catch (UnsupportedEncodingException ex) {
            result = false;
        } catch (IOException ex) {
            result = false;
        }

        return result;
    }

    /**
     * Web service operation
     * Return true if operation successful and a matching was found
     * if no matching enchere nothing is changed
     */
    @WebMethod(operationName = "modifierEnchere")
    public boolean modifierEnchere(@WebParam(name = "nomEnchere")
    final String nomEnchere, @WebParam(name = "montantMax")
    final int montantMax, @WebParam(name = "nomUtilisateur")
    final String nomUtilisateur) {
        FileInputStream fis;
        FileOutputStream fos;
        OutputStreamWriter out;
        StringBuilder sb = new StringBuilder();
        DataInputStream in;
        BufferedReader br;
        String line;
        String name;
        boolean result = false;
        try {
            fis = new FileInputStream(ENCHERE_FILE_URL);
            in = new DataInputStream(fis);
            br = new BufferedReader(new InputStreamReader(in));

            while((line = br.readLine()) != null) {
                name = line.split(";")[0];
                if(name.equals(nomEnchere)) {
                    sb.append(String.format("%s;%d;%s\n", nomEnchere, montantMax, nomUtilisateur));
                    result = true;
                }
                else {
                    sb.append(line);
                    sb.append("\n");
                }
            }

            fos = new FileOutputStream(ENCHERE_FILE_URL, false);
            out = new OutputStreamWriter(fos, "UTF-8");
            out.write(sb.toString());
            out.close();
            fos.close();
        } catch (FileNotFoundException ex) {
            result = false;
        } catch (UnsupportedEncodingException ex) {
            result = false;
        } catch (IOException ex) {
            result = false;
        }

        return result;
    }

        /**
     * Web service operation
     * Return true if operation successful
     */
    @WebMethod(operationName = "ajouterUser")
    public boolean ajouterUser(@WebParam(name = "nom")
    final String nom, @WebParam(name = "password")
    final String password) {
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(USER_FILE_URL, true);
            OutputStreamWriter out = new OutputStreamWriter(fos, "UTF-8");
            out.write(String.format("%s;%s\n", nom, password));
            out.close();
            fos.close();
            return true;
        } catch (FileNotFoundException ex) {
            return false;
        } catch (UnsupportedEncodingException ex) {
            return false;
        } catch (IOException ex) {
            return false;
        }
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "ajouterEnchere")
    public boolean ajouterEnchere(@WebParam(name = "nomEnchere")
    final String nomEnchere, @WebParam(name = "montantMax")
    final int montantMax, @WebParam(name = "nomUtilisateur")
    final String nomUtilisateur) {
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(ENCHERE_FILE_URL, true);
            OutputStreamWriter out = new OutputStreamWriter(fos, "UTF-8");
            out.write(String.format("%s;%d;%s\n", nomEnchere, montantMax, nomUtilisateur));
            out.close();
            fos.close();
            return true;
        } catch (FileNotFoundException ex) {
            return false;
        } catch (UnsupportedEncodingException ex) {
            return false;
        } catch (IOException ex) {
            return false;
        }
    }

}
